/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlo.util;

import java.sql.*;

/**
 *
 * @author Remy
 */
public class Conexao 
{
    private static Connection conn;
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/ucandb";
    private static String utilizador = "root";
    private static String senha = "root";
    
    public Conexao()
    {}
    
    public static Connection getConnection() throws SQLException, ClassNotFoundException
    {
        if(conn == null)
        {
            Class.forName(driver);
            return DriverManager.getConnection(url, utilizador, senha);
        }
        return conn;
    }

    public Statement getStatement() throws SQLException, ClassNotFoundException
    {
        return getConnection().createStatement();
    }

    public Statement getPreparedStatement(String sql) throws SQLException, ClassNotFoundException
    {
        return getConnection().prepareStatement(sql);
    }
    
    /*public void displayDbProperties(){
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;
          try{
               conn= this.getConnection();
               if(conn!=null){
                    dm = conn.getMetaData();
                    System.out.println("Driver Information");
                    System.out.println("\tDriver Name: "+ dm.getDriverName());
                    System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                    System.out.println("\nDatabase Information ");
                    System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                    System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                    System.out.println("Avalilable Catalogs ");
                    rs = dm.getCatalogs();
                    while(rs.next()){
                         System.out.println("\tcatalog: "+ rs.getString(1));
                    } 
                    rs.close();
                    rs = null;
                    closeAll();
               }else System.out.println("Error: No active Connection");
          }catch(Exception e){
               e.printStackTrace();
          }
          dm=null;
     }*/    

    public void closeAll() throws SQLException
    {
        if(conn != null)
        {
            conn.close();
        }
    }
    
    public static void closeConnection(Connection connection, Statement stmt, ResultSet rs)throws Exception
    {
        close(connection, stmt, rs);
    }
    
    public static void closeConnection(Connection connection, Statement stmt)throws Exception
    {
        close(connection, stmt, null);
    }
    
    public static void closeConnection(Connection connection)throws Exception
    {
        close(connection, null, null);
    }
    
    private static void close(Connection connection, Statement stmt, ResultSet rs)throws Exception
    {
        try{
            if(rs!=null)
            {
                rs.close();
            }
            if(stmt!=null)
            {
                stmt.close();
            }
            
            if(connection!=null)
            {
                connection.close();
            }
        }
        
        catch(Exception e)
        {
            throw new Exception(e.getMessage());
        }
    }
    /*public static void main(String[] args)
    {
        Conexao conexao = new Conexao();
        conexao.displayDbProperties();
    }*/
}
